package com.tl.web03.listener;


import com.tl.web03.config.Config;
import com.tl.web03.model.entity.User;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.util.HashMap;

/**
 * 监听程序的初始化
 */
@WebListener
public class WebInitListener implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {

    public WebInitListener() {
    }
    //当servltcontext初始化的时候执行
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        HashMap<String, User> userMap = new HashMap<String,User>();
        ServletContext application = sce.getServletContext();
        application.setAttribute(Config.USER_MAP,userMap);
        System.out.println("用户map被初始化了");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        /* This method is called when the servlet Context is undeployed or Application Server shuts down. */
    }

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
    }

    @Override
    public void attributeAdded(HttpSessionBindingEvent sbe) {
        /* This method is called when an attribute is added to a session. */
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent sbe) {
        /* This method is called when an attribute is removed from a session. */
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent sbe) {
        /* This method is called when an attribute is replaced in a session. */
    }
}
